﻿using Sidvall.Serialization;
using System.Collections.Generic;
using System.Linq;

namespace Sidvall.IO
{
    public class FileTypeChoiceCollection : System.Collections.ObjectModel.Collection<FileTypeChoice>
    {
        #region Public Members

        #region Add

        public void Add(string name, params string[] filters)
        {
            Add(name, filters.AsEnumerable());
        }
        public void Add(string name, IEnumerable<string> filters)
        {
            var item = new FileTypeChoice()
            {
                Name = name
            };
            item.FileTypeFilter.AddRange(filters, true);
            Add(item);
        }

        #endregion
        #region AddRange

        public void AddRange(params FileTypeChoice[] filters)
        {
            AddRange(filters.AsEnumerable());
        }
        public void AddRange(IEnumerable<FileTypeChoice> filters)
        {
            if (filters == null)
                return;
            foreach (var filter in filters)
                Add(filter);
        }

        #endregion
        #region GetFileTypeFilters

        public string[] GetFileTypeFilters()
        {
            var items = new System.Collections.Generic.HashSet<string>();
            foreach (var fileTypeChoice in this)
            {
                foreach (var fileTypeFilter in fileTypeChoice.FileTypeFilter)
                {
                    if (!items.Contains(fileTypeFilter))
                        items.Add(fileTypeFilter);
                }
            }
            return items.ToArray();
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            var sb = new System.Text.StringBuilder();
            foreach (var fileTypeChoice in this)
            {
                if (sb.Length > 0)
                    sb.Append("|");
                sb.Append(fileTypeChoice.ToString());
            }
            return sb.ToString();
        }

        #endregion

        #region Parse

        public static FileTypeChoiceCollection Parse(string filter)
        {
            var items = new FileTypeChoiceCollection();
            if (!string.IsNullOrWhiteSpace(filter))
            {
                var parts = filter.Split("|", System.StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < parts.Length - 1; i += 2)
                {
                    var filterList = new System.Collections.Generic.List<string>();
                    var part1 = parts[i];
                    var part2 = parts[i + 1];
                    var pos = part1.IndexOf("(", System.StringComparison.OrdinalIgnoreCase);
                    if (pos > 0)
                        part1 = part1.Substring(0, pos - 1).Trim();
                    var subParts = part2.Split(";", System.StringSplitOptions.RemoveEmptyEntries);
                    foreach (var subPart in subParts)
                    {
                        if (subPart.StartsWith("*", System.StringComparison.OrdinalIgnoreCase))
                            filterList.Add(subPart.Substring(1));
                        else
                            filterList.Add(subPart);
                    }
                    items.Add(part1, filterList);
                }
            }
            return items;
        }
        #endregion

        #endregion
    }
}
